package singlethread;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;

/**
 * Finds how many times a keyword appears in the web pages.  
 * It uses a hashmap of keyword/hit pairs to keep track.
 * @author Darin
 * @author Kirk Leonard
 * @author John Patanian
 * @version Autumn 2009
 */
public class DataCollector 
{
  /**
  * Collection for holding the keywords and hits.
  */

  private Map<String, Integer> my_map;

  /**
  * The page count.
  */
  private int my_page_count;

  /**
  * The number of words found.
  */
  private int my_number_of_words;

  /**
  * Constructor takes pageCount and uses it to generate filenames that it will open.
  * @param the_page_count The number of pages crawled.
  */
  public DataCollector(final int the_page_count) 
  {
    my_page_count = the_page_count;
    my_map = new HashMap<String, Integer>();
    loadKeyWords();
    processPages();
  }

  /**
  * Opens each page that has been cached locally and looks for keywords.
  */
  private void processPages() 
  {
    for (int i = 0; i < my_page_count; i++) 
    {
      File cache_file = null;
      Scanner input = null;
      // open a file
      try 
      {
        cache_file = new File("file" + i);
        input = new Scanner(cache_file);
      } 
      catch (final FileNotFoundException e) 
      {
        System.out.println(e);
      }

      while (input.hasNext()) 
      {
        my_number_of_words++;
        final String a_word = input.next().toLowerCase();

        if (my_map.containsKey(a_word)) 
        {
          my_map.put(a_word, my_map.get(a_word) + 1);
        }
      }
      
      input.close();
      cache_file.delete();
    }
  }

  /**
  * Used to calculate the average number of keywords per page.
  * @return Returns the number of words.
  */
  public int getNumWords() 
  {
    return my_number_of_words;
  }

  /**
  * Used so the ui can print out a list of keywords and how often they're found.
  * @return returns a set with word counts.
  */
  public Set<Entry<String, Integer>> getWordCounts()
  {
    return my_map.entrySet();
  }

  /**
   * Load the key words from keywords.txt.
   */
  private void loadKeyWords() 
  {
    Scanner word_scanner = null;
    
    try 
    {
      word_scanner = new Scanner(new File("keyWords.txt"));
    }
    catch (final FileNotFoundException e) 
    {
      System.out.println(e);
    }

    while (word_scanner.hasNext())
    {
      my_map.put(word_scanner.next(), new Integer(0));
    }
    
    word_scanner.close();
  }
}
